package com.vo;

import java.util.List;
import java.util.Map;

public class Site {
	private  List menu;// 菜单
	private String menuHtml;//菜单
	private static Site intance = null;

	public  List getMenu() {
		return menu;
	}

	public  void setMenu(List menu) {
		this.menu = menu;
	}

	public String getMenuHtml() {
		return menuHtml;
	}

	public void setMenuHtml(String menuHtml) {
		this.menuHtml = menuHtml;
	}

	synchronized public static Site getInstance() throws Exception {
		if (intance == null) {
			intance = new Site();
			intance.init();
		}
		return intance;
	}

	public void init() throws Exception {
		this.menuHtml="<ul>";
		if(menu!=null)
		for(Map v:(List<Map>)menu){
			String code=(String)v.get("code");
			String pCode=(String)v.get("pCode");
			String name=(String)v.get("name");
			String url=(String)v.get("url");
			String target=(String)v.get("target");
			String isLeaf=(String)v.get("isLeaf");
			if(url.indexOf("?")>0)
				url+="&pageTitle="+name;
			else
				url+="?pageTitle="+name;
			if(code.length()==3&&pCode==null&&isLeaf.equals("Y")){
				this.menuHtml+="<li><a href=\""+url+"\" >"+name+"</a></li>\n";
			}else if(code.length()==3&&pCode==null&&isLeaf.equals("N")){
				this.menuHtml+="<li><a href=\""+url+"\" >"+name+"</a><ul>";
				for(Map v1:(List<Map>)menu){
					String code1=(String)v1.get("code");
					String pCode1=(String)v1.get("pCode");
					String name1=(String)v1.get("name");
					String url1=(String)v1.get("url");
					String target1=(String)v1.get("target");
					String isLeaf1=(String)v1.get("isLeaf");
					if(code1.length()==6&&pCode1.equals(code)&&isLeaf1.equals("Y")){
						if(url1.indexOf("?")>0)
							url1+="&pageTitle="+name1;
						else
							url1+="?pageTitle="+name1;
						this.menuHtml+="<li><a href=\""+url1+"\" >"+name1+"</a></li>\n";
					}else if(code1.length()==6&&pCode1.equals(code)&&isLeaf1.equals("N")){
						this.menuHtml+="<li><a href=\""+url1+"\" >"+name1+"</a><ul>";
						for(Map v2:(List<Map>)menu){
							String code2=(String)v2.get("code");
							String pCode2=(String)v2.get("pCode");
							String name2=(String)v2.get("name");
							String url2=(String)v2.get("url");
							String target2=(String)v2.get("target");
							String isLeaf2=(String)v2.get("isLeaf");
							if(code2.length()==9&&pCode2.equals(code1)&&isLeaf2.equals("Y")){
								if(url2.indexOf("?")>0)
									url2+="&pageTitle="+name2;
								else
									url2+="?pageTitle="+name2;
								this.menuHtml+="<li><a href=\""+url2+"\" >"+name2+"</a></li>\n";
							}
						}
						this.menuHtml+="</ul></li>\n";
					}
				}
				this.menuHtml+="</ul></li>\n";
			}
				
		}
		this.menuHtml+="</ul>";
		System.out.println(menuHtml);
	}

}
